$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/workflow/actFlow/getWorkFlowRunning',
        datatype: "json",
        colModel: [
            {label: '任务编号', name: 'id', index: 'id', width: 95},
            {label: '名称', name: 'processDefinition.name', index: 'processDefinition.name', width: 95},
            {label: '部署id', name: 'processDefinitionId', index: 'processDefinitionId', width: 120},
            {label: 'key', name: 'processDefinition.key', index: 'processDefinition.key', width: 95},
            {label: '版本', name: 'processDefinition.version', index: 'processDefinition.version', width: 95},
            {
                label: '操作', width: 80, formatter: function (value, options, row) {
                return '<a class="btn btn-delete"  onclick=vm.getHistoryInfo(\"' + row.id + '\") style="display: inline;">历史</a>'
            }
            }
        ],
		viewrecords: true,
        height: 365,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{

        },
		showList: true,
		title: null,
		model: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
            vm.model = {};
		},
		update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		save: function (event) {
			// var url = vm.legalProvisions.id == null ? "legalprovisions/save" : "legalprovisions/update";
            var url = "workflow/actModel/createActModel";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                // url: url,
			    contentType: "application/json",
			     data: JSON.stringify(vm.model),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            if(ids.length>1){
                alert("请选择一条记录");
                return;
            }
           var len= ids[0].indexOf(":",1);
            var key=ids[0].substring(0,len);
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "workflow/actFlow/delteProcessDefinitionByKey/"+key,
                    contentType: "application/json",
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getHistoryInfo: function (processInstanceId) {
            vm.showList = false;
            vm.model = {};
            $("#legaljqGrid").jqGrid({
                url: baseURL + '/workflow/actFlow/histoicFlow?procInsId=' + processInstanceId,
                datatype: "json",
                colModel: [
                    {label: 'Id', name: 'processInstanceId', index: 'processInstanceId', width: 80},
                    {label: '节点Id', name: 'activityId', index: 'activityId', width: 80},
                    {label: '节点名称', name: 'activityName', index: 'activityName', width: 80},
                    {label: '节点类型', name: 'activityType', index: 'activityType', width: 80},
                    {label: '审批人', name: 'assignee', index: 'assignee', width: 80},
                    {label: '开始时间', name: 'startTime', index: 'startTime', width: 80},
                    {label: '结束时间', name: 'endTime', index: 'endTime', width: 80}
                ],
                viewrecords: true,
                height: 365,
                rowNum: 10,
                rowList: [10, 30, 50],
                rownumbers: true,
                rownumWidth: 45,
                autowidth: true,
                multiselect: false,
                pager: "#legaljqGridPager",
                jsonReader: {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order"
                },
                gridComplete: function () {
                    //隐藏grid底部滚动条
                    $("#legaljqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
                    $("#legaljqGrid").setGridWidth($(window).width() * 0.99);
                }
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "legalprovisions/info/" + id, function (r) {
                vm.legalProvisions = r.legalProvisions;
            });
        },
        goBack: function () {
            window.location.href = "runprocess.html";
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{'queryname':vm.q.name}
            }).trigger("reloadGrid");
		}
	}
});